package com.xtianzhuang.www.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.Map;

import javax.activation.MimetypesFileTypeMap;

public class PostmanUtils {

	private static final int CONNECTION_TIMEOUT = 5000;
	private static final int READ_TIMEOUT = 30000;
	private static final boolean USE_CACHE = false;
	private static final String BOUNDARY = "----------------1234567890987654321";

	public static final String METHOD_POST = "POST";
	public static final String METHOD_GET = "GET";

	public static final String CONTENT_TYPE_FORM_DATA = "multipart/form-data;";
	public static final String CONTENT_TYPE_JSON = "application/json;charset=UTF-8;";

	private static HttpURLConnection CONNECTION;

	public static String sendGet(String url) throws IOException {
		return new String(send(url, METHOD_GET, CONTENT_TYPE_JSON, null, null));
	}

	public static byte[] sendGet(String url, Map<String, String> headerMap) throws IOException {
		return send(url, METHOD_GET, CONTENT_TYPE_JSON, null, null);
	}

	public static String sendGet(String url, String contentType, Map<String, String> headerMap,
			Map<String, Object> bodyMap) throws IOException {
		return new String(send(url, METHOD_GET, contentType, headerMap, bodyMap));
	}

	public static String sendGetJson(String url, Map<String, String> headerMap, Map<String, Object> bodyMap)
			throws IOException {
		return new String(send(url, METHOD_GET, CONTENT_TYPE_JSON, headerMap, bodyMap));
	}

	public static String sendGetJson(String url, Map<String, Object> bodyMap) throws IOException {
		return new String(send(url, METHOD_GET, CONTENT_TYPE_JSON, null, bodyMap));
	}

	public static String sendPost(String url, String contentType, Map<String, String> headerMap,
			Map<String, Object> bodyMap) throws IOException {
		return new String(send(url, METHOD_POST, contentType, headerMap, bodyMap));
	}

	public static String sendPostJson(String url, Map<String, String> headerMap, Map<String, Object> bodyMap)
			throws IOException {
		return new String(send(url, METHOD_POST, CONTENT_TYPE_JSON, headerMap, bodyMap));
	}

	public static String sendPostJson(String url, Map<String, Object> bodyMap) throws IOException {
		return new String(send(url, METHOD_POST, CONTENT_TYPE_JSON, null, bodyMap));
	}

	public static byte[] send(String url, String method, String contentType, Map<String, String> headerMap,
			Map<String, Object> bodyMap) throws IOException {

		initHttpConnection(url, method);

		setHeader(contentType, headerMap);

		// setting json body
		if (bodyMap != null) {
			setBody(contentType, bodyMap);
		}

		return getResponse();
	}


	public static void initHttpConnection(String url, String method) {

	}

	public static void setHeader(String contentType, Map<String, String> headerMap) {

	}

	public static void setBody(String contentType, Map<String, Object> headerMap) {

	}


	private static byte[] getResponse() throws IOException {
		// read return message
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		InputStream inputStream = CONNECTION.getInputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while (-1 != (len = inputStream.read(buffer))) {
			byteArrayOutputStream.write(buffer, 0, len);
		}
		inputStream.close();
		return byteArrayOutputStream.toByteArray();
	}


	private static String getContentTypeByFile(File file) {
		String fileName = file.getName();
		String contentType = new MimetypesFileTypeMap().getContentType(file);
		if (isEmpty(contentType)) {
			contentType = "application/octet-stream";
		} else {
			String[] endSuffixes = {".png", ".gif", ".ico", ".jpg", ".jpeg", ".jpe"};
			String[] types = {"image/png", "image/gif", "image/image/x-icon", "image/jpeg"};
			contentType = "application/octet-stream";
			for (int i = 0; i < endSuffixes.length; i++) {
				if (fileName.toLowerCase().endsWith(endSuffixes[i])) {
					contentType = types[Math.min(i, types.length - 1)];
					break;
				}
			}
		}

		return contentType;
	}

	private static boolean isEmpty(String s) {
		return null == s || "".equals(s.trim());
	}
}
